/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-3-18
 */
package code.extract;

public class SegmentItem {
	/**
	 * if this is set, this segment item is a code segment
	 */
	public static final int CODE_BIT = 1;
	public static final int TEXT_BIT = 1 << 1;
	/**
	 * if this is set, this segment item is a spam, like nevigator
	 */
	public static final int SPAM_BIT = 1 << 2;
	private String content = null;
	private String beginTag = null;
	private String endTag = null;
	private int index = -1;
	private int contentType = 0;
	public SegmentItem(SegmentItem original){
		if (original != null){
			this.content = new String(original.content);
			this.beginTag = new String(original.beginTag);
			this.endTag = new String(original.endTag);
			this.index = original.index;
			this.contentType = original.contentType;
		}
	}
	public SegmentItem(String content, String beginTag, String endTag, int index){
		this.content = content;
		this.beginTag = beginTag;
		this.endTag = endTag;
		this.setIndex(index);
		this.contentType = 0;
	}
	public static SegmentItem mergeTwoSegment(SegmentItem firstSeg, SegmentItem secondSeg){
		SegmentItem result = new SegmentItem(firstSeg);
		if (secondSeg.isCodeSegment()){
			result.setCodeType();
		}
		result.setEndTag(secondSeg.getEndTag());
		String content = result.getContent() + secondSeg.getContent();
		result.setContent(content);
		return result;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getContent() {
		return content;
	}
	public void setBeginTag(String beginTag) {
		this.beginTag = beginTag;
	}
	public String getBeginTag() {
		return beginTag;
	}
	public void setEndTag(String endTag) {
		this.endTag = endTag;
	}
	public String getEndTag() {
		return endTag;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public int getIndex() {
		return index;
	}
	public void setCodeType(){
		contentType = contentType | CODE_BIT;
	}
	public void resetCodeType(){
		contentType = contentType & (~CODE_BIT);
	}
	public boolean isCodeSegment(){
		if ((contentType & CODE_BIT) != 0){
			return true;
		}
		return false;
	}
	public void setSpamType(){
		contentType = contentType | SPAM_BIT;
	}
	public void resetSpamType(){
		contentType = contentType & (~SPAM_BIT);
	}
	public boolean isSpamSegment(){
		if ((contentType & SPAM_BIT) != 0){
			return true;
		}
		return false;
	}
}
